package minLength;

import java.util.List;
import java.util.Stack;

public class Solution {
    public int minLength(String s) {
        // 删除"AB"
        // 栈
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            if(s.charAt(i) == 'B'&&!stack.isEmpty()) {
                if(stack.peek()=='A') {
                    stack.pop();
                } else {
                    stack.push(s.charAt(i));
                }
            } else if (s.charAt(i) == 'D'&&!stack.isEmpty()) {
                if(stack.peek() == 'C') {
                    stack.pop();
                } else {
                    stack.push(s.charAt(i));
                }
            }else {
                stack.push(s.charAt(i));
            }
        }
        return stack.size();
    }
}
